package com.liu.sportplat.controller;

import com.alibaba.fastjson.JSON;
import com.liu.sportplat.bean.QueryInfo;
import com.liu.sportplat.bean.User;
import com.liu.sportplat.dao.UserMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserMapping userMapping;

    @RequestMapping("/alluser")
    public String getUserList(QueryInfo queryInfo){
        //首先获取最大列表数和当前编号
        int number = userMapping.getUserCounts("%" + queryInfo.getQueryName() + "%");
        int pageStart=(queryInfo.getPageNum()-1)* queryInfo.getPageSize();

        //得到所有用户
        List<User> users = userMapping.getAllUser("%" + queryInfo.getQueryName() + "%",
                pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("number",number);
        res.put("data",users);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    //根据角色名查找
    @RequestMapping("/alluserRole")
    public String getUserListRole(QueryInfo queryInfo){
        System.out.println(queryInfo);
        //首先获取最大列表数和当前编号
        int number = userMapping.getUserCountsRole("%" + queryInfo.getQueryRole() + "%");
        int pageStart=(queryInfo.getPageNum()-1)* queryInfo.getPageSize();

        //得到所有用户
        List<User> users = userMapping.getAllUserRole("%" + queryInfo.getQueryRole() + "%",
                pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("number",number);
        res.put("data",users);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    //修改用户状态
    @RequestMapping("/userState")
    public String updateUserState(@RequestParam("id") Integer id,
                                  @RequestParam("state") Boolean state){
        int i=userMapping.updateState(id,state);
        System.out.println("用户编号："+id);
        System.out.println("用户状态："+state);
        //是否修改成功，成功返alluser回success，失败则error
        String str=i>0?"success":"error";
        System.out.println(str);
        return str;
    }

    //添加用户
    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){
        System.out.println("/addUser-User:"+user);
        int i = userMapping.addUser(user);
        String str = i > 0 ? "success" : "error";
        return str;
    }

    //删除用户
    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        System.out.println(id);
        int i = userMapping.deleteUser(id);
        String str = i >0?"success":"error";
        return str;
    }

    //得到修改用户
    @RequestMapping("/getUpdate")
    public String getUpdateUser(int id){
        System.out.println("编号:"+id);
        User updateUser = userMapping.getUpdateUser(id);
        String users_json = JSON.toJSONString(updateUser);
        return users_json;
    }

    //修改用户
    @RequestMapping("/editUser")
    public String editUser(@RequestBody User user){
        System.out.println(user);
        int i = userMapping.editUser(user);
        String str = i >0?"success":"error";
        return str;
    }
}
